<template>
  <div class="main-box">
    <el-button
      icon="el-icon-plus"
      type="primary"
      style="margin-bottom: 16px"
      @click="goAdd"
    >
      {{ $t('添加') }}
    </el-button>
    <el-table
      ref="treeTable"
      :data="menuList"
      row-key="id"
      :tree-props="{ children: 'children' }"
      @row-click="handleRow"
    >
      <el-table-column label="ID" prop="id"></el-table-column>
      <el-table-column :label="$t('菜单名称')" prop="title"></el-table-column>
      <el-table-column :label="$t('菜单图标')" prop="icon"></el-table-column>
      <el-table-column
        :label="$t('操作')"
        align="right"
        fixed="right"
        width="150"
      >
        <template slot-scope="scope">
          <el-tooltip effect="dark" :content="$t('添加子菜单')" placement="top">
            <el-button
              v-if="+scope.row.level < 2"
              icon="el-icon-plus"
              type="text"
              @click.stop="goAdd('child')"
            ></el-button>
          </el-tooltip>
          <EditBtn @click.stop="goEdit(scope.row)" />
          <DelBtn @success="goDel(scope.row)" />
        </template>
      </el-table-column>
    </el-table>
    <BaseDialog
      :dialog-show.sync="dialogShow"
      :title="addForm.id ? $t('编辑菜单') : $t('添加菜单')"
      width="50"
      footer
      @submit="submit"
    >
      <el-form
        :model="addForm"
        ref="addForm"
        :rules="formRules"
        label-position="top"
      >
        <el-form-item :label="$t('上级菜单')" prop="parent_id">
          <el-cascader
            :options="options"
            :show-all-levels="false"
            v-model="addForm.parent_id"
          ></el-cascader>
        </el-form-item>
        <el-form-item :label="$t('菜单名称')" prop="title">
          <el-input v-model="addForm.sort"></el-input>
        </el-form-item>
        <el-form-item :label="$t('控制器名称')" prop="controller">
          <el-input v-model="addForm.controller"></el-input>
        </el-form-item>
        <el-form-item :label="$t('操作名称')" prop="action">
          <el-input v-model="addForm.action"></el-input>
        </el-form-item>
        <el-form-item ::label="$t('排序')" prop="sort">
          <el-input v-model="addForm.sort"></el-input>
        </el-form-item>
        <el-form-item :label="$t('菜单类型')" prop="type">
          <el-radio-group v-model="addForm.type">
            <el-radio :label="1">{{ $t('只作为菜单') }}</el-radio>
            <el-radio :label="2">{{ $t('可访问菜单') }}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item :label="$t('是否为菜单')" prop="is_menu">
          <el-radio-group v-model="addForm.is_menu">
            <el-radio :label="1">{{ $t('菜单') }}</el-radio>
            <el-radio :label="2">{{ $t('不是菜单') }}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item :label="$t('菜单图标')" prop="icon">
          <el-input v-model="addForm.icon"></el-input>
        </el-form-item>
        <el-form-item :label="$t('额外参数')">
          <el-input v-model="addForm.params"></el-input>
        </el-form-item>
      </el-form>
    </BaseDialog>
  </div>
</template>
<script>
import menuApi from '@/api/system/navMenu'
export default {
  data() {
    return {
      dialogShow: false,
      options: [],
      menuList: [],
      addForm: {
        id: '',
        title: '',
        controller: '',
        action: '',
        parent_id: '',
        sort: '',
        type: '',
        param: '',
        is_menu: ''
      },
      formRules: {
        title: [
          {
            required: true,
            message: this.$t('请输入菜单名称'),
            trigger: 'blur'
          }
        ],
        controller: [
          {
            required: true,
            message: this.$t('请输入控制器名'),
            trigger: 'blur'
          }
        ],
        action: [
          { required: true, message: this.$t('请输入操作名'), trigger: 'blur' }
        ],
        parent_id: [
          {
            required: true,
            message: this.$t('请选择上级菜单'),
            trigger: 'blur'
          }
        ],
        sort: [
          { required: true, message: this.$t('请输入排序'), trigger: 'blur' }
        ],
        type: [
          { required: true, message: this.$t('选择菜单类型'), trigger: 'blur' }
        ],
        is_menu: [
          {
            required: true,
            message: this.$t('选择是否为菜单'),
            trigger: 'blur'
          }
        ]
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    // 点击行展开
    handleRow(row) {
      this.$refs.treeTable.toggleRowExpansion(row)
    },
    goAdd() {
      this.dialogShow = true
    },
    goEdit() {
      this.dialogShow = true
    },
    submit() {},
    goDel() {},
    async getList() {
      const res = await menuApi.list()
      if (res.code == 0) {
        this.menuList = res.data
      }
    }
  }
}
</script>
<style scoped lang="scss">
.el-button {
  ::v-deep {
    .el-icon-plus {
      font-weight: bold;
    }
  }
}
</style>
